package com.yanfan.community.building.service.impl;

import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.community.building.domain.BuildingTreeSelect;
import com.yanfan.community.building.domain.CommunityBuilding;
import com.yanfan.community.building.mapper.CommunityBuildingMapper;
import com.yanfan.community.building.service.ICommunityBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 建筑Service业务层处理
 *
 * @author li
 * @date 2025-07-03
 */
@Service
public class CommunityBuildingServiceImpl implements ICommunityBuildingService {
    @Autowired
    private CommunityBuildingMapper communityBuildingMapper;

    /**
     * 查询建筑
     *
     * @param buildingId 建筑主键
     * @return 建筑
     */
    @Override
    public CommunityBuilding selectSysBuildingByBuildingId(Long buildingId) {
        return communityBuildingMapper.selectSysBuildingByBuildingId(buildingId);
    }

    /**
     * 查询建筑列表
     *
     * @param communityBuilding 建筑
     * @return 建筑
     */
    @Override
    public List<CommunityBuilding> selectSysBuildingList(CommunityBuilding communityBuilding) {
        return communityBuildingMapper.selectSysBuildingList(communityBuilding);
    }

    /**
     * 新增建筑
     *
     * @param communityBuilding 建筑
     * @return 结果
     */
    @Override
    public int insertSysBuilding(CommunityBuilding communityBuilding) {
        Long parentId = communityBuilding.getParentId();
        CommunityBuilding parent = communityBuildingMapper.selectSysBuildingByBuildingId(parentId);
        communityBuilding.setAncestors(parent.getAncestors() + "," + parentId);

        communityBuilding.setCreateTime(DateUtils.getNowDate());
        communityBuilding.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return communityBuildingMapper.insertSysBuilding(communityBuilding);
    }

    /**
     * 修改建筑
     *
     * @param communityBuilding 建筑
     * @return 结果
     */
    @Override
    public int updateSysBuilding(CommunityBuilding communityBuilding) {
        communityBuilding.setUpdateTime(DateUtils.getNowDate());
        communityBuilding.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return communityBuildingMapper.updateSysBuilding(communityBuilding);
    }

    /**
     * 批量删除建筑
     *
     * @param buildingIds 需要删除的建筑主键
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByBuildingIds(Long[] buildingIds) {
        return communityBuildingMapper.deleteSysBuildingByBuildingIds(buildingIds);
    }

    /**
     * 删除建筑信息
     *
     * @param buildingId 建筑主键
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByBuildingId(Long buildingId) {
        return communityBuildingMapper.deleteSysBuildingByBuildingId(buildingId);
    }

    @Override
    public List<BuildingTreeSelect> buildingTree(CommunityBuilding communityBuilding) {
        communityBuilding.setShowChild(true);
        List<CommunityBuilding> communityBuildings = communityBuildingMapper.selectSysBuildingList(communityBuilding);
        return buildCommunityBuildingTreeSelect(communityBuildings);
    }

    @Override
    public List<CommunityBuilding> selectSysBuildingListByParentId(Long buildingId) {
        return communityBuildingMapper.selectSysBuildingListByParentId(buildingId);
    }

    @Override
    public List<CommunityBuilding> selectChildBuildingListWithOwnerByParentId(Long buildingId) {
        return communityBuildingMapper.selectChildBuildingListWithOwnerByParentId(buildingId);
    }

    /**
     * 构建前端所需要树结构
     *
     * @return 树结构列表
     */
    public List<CommunityBuilding> buildCommunityBuildingTree(List<CommunityBuilding> communityBuildings) {
        List<CommunityBuilding> returnList = new ArrayList<CommunityBuilding>();
        List<Long> tempList = communityBuildings.stream().map(CommunityBuilding::getBuildingId).collect(Collectors.toList());
        for (CommunityBuilding communityBuilding : communityBuildings) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(communityBuilding.getParentId())) {
                recursionFn(communityBuildings, communityBuilding);
                returnList.add(communityBuilding);
            }
        }
        if (returnList.isEmpty()) {
            returnList = communityBuildings;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @return 下拉树结构列表
     */

    public List<BuildingTreeSelect> buildCommunityBuildingTreeSelect(List<CommunityBuilding> communityBuildings) {
        List<CommunityBuilding> CommunityBuildingTrees = buildCommunityBuildingTree(communityBuildings);
        return CommunityBuildingTrees.stream().map(BuildingTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<CommunityBuilding> list, CommunityBuilding t) {
        // 得到子节点列表
        List<CommunityBuilding> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CommunityBuilding tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CommunityBuilding> list, CommunityBuilding t) {
        return !getChildList(list, t).isEmpty();
    }

    /**
     * 得到子节点列表
     */
    private List<CommunityBuilding> getChildList(List<CommunityBuilding> list, CommunityBuilding t) {
        List<CommunityBuilding> tlist = new ArrayList<CommunityBuilding>();
        Iterator<CommunityBuilding> it = list.iterator();
        while (it.hasNext()) {
            CommunityBuilding n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getBuildingId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
